import { cloneDeep } from 'lodash-es'
export function util() {
    const goUp = (list: any, sort: number) => {
        // sort从1开始
        // console.log('上移row', sort)
        const tempList = cloneDeep(list)
        const target = tempList.find(item => item.sort === sort)
        //删除当前项
        tempList.splice(sort - 1, 1)
        tempList[sort - 2].sort = sort //换下来的一项sort
        //往上拼接一项
        tempList.splice(sort - 2, 0, toRaw(target))
        tempList[sort - 2].sort = sort - 1 //上移的一项sort
        // console.log('上移后', tempList)
        return tempList
        // 
    }
    const goDown = (list: any, sort: number) => {
        // console.log('下移row', sort)
        const tempList = cloneDeep(list)
        const target = tempList.find(item => item.sort === sort)
        //删除当前项
        tempList.splice(sort - 1, 1)
        tempList[sort - 1].sort = sort //换上来的一项sort
        //往下拼接一项
        tempList.splice(sort, 0, toRaw(target))
        tempList[sort].sort = sort + 1 //下移的一项sort
        // console.log('下移后', tempList)
        return tempList
    }

    const findIndexFromList = (list: any) => {
        const index = list.reduce((pre, cur, index) => {
            const startTime = new Date(cur.activeStart).getTime()
            const endTime = new Date(cur.activeEnd).getTime()
            const now = new Date().getTime()
            if(now < endTime &&  now > startTime) {
                return index
            } else {
                return 0
            }
        }, 0)
    }

    return { goUp, goDown, findIndexFromList }
}
